<template>
  <div>
    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      :data="tableDataFn"
      :header-cell-style="{
        background: '#E0E0E0',
      }"
      stripe
      style="width: 100%"
      :fit="true"
    >
      <el-table-column type="index" width="50" label="序号"> </el-table-column>
      <template v-for="item in tableColumns">
        <el-table-column
          :prop="item.prop"
          :label="item.label"
          :sortable="item.sortable"
          :width="item.width"
          align="center"
          v-if="item.isShow"
          :key="item.prop"
        >
        </el-table-column>
      </template>
    </el-table>
    <v-pagination
      :dataTotal="postData.dataTotal"
      :curPage="postData.curPage"
      :pageSize="postData.pageSize"
      @getCurPage="getPaginationCurPage"
      @getPageSize="getPaginationPageSize"
    >
    </v-pagination>
  </div>
</template>

<script>
import { getJavaServerData } from "@/api/index.js";
import vPagination from "itemMonitorCom/Pagination.vue";
import tableColumns from "./DbSessionColumns.js";
import bus from "common/bus.js";
import { GetItem } from "@/utils/dataStore"
export default {
  name: "dbTableSpace",
  components: {
    vPagination,
  },
  data() {
    return {
      postData: {
        curPage: 1,
        pageSize: 5,
        dataTotal: 0,
      },
      tableData: [],

      tableColumns: tableColumns,
      SearchIPArr: [],
      searchJson: {},
      SearchIP: "",
      timerName: "MonitorInfoDataBaseSqlServerSession",
      timerEventName: "timerMonitorInfoDataBaseSqlServerSession",
      timerInterval: 1000 * 60 * 5,
      loading: true,
      Ioitem: {
        instance: "",
        ip: "",
      },
      selectedMonID: "",
    };
  },
   created() {
    GetItem(sessionStorage.getItem('PushId')).then(_ => {
      this.selectedMonID =_[0].articleAddress;
      this.getTableData();
    })
    

    bus.$emit("timerNewTodo", {
      name: this.timerName,
      eventName: this.timerEventName,
      interval: this.timerInterval,
      intervalNow: 0,
    });

    bus.$on(this.timerEventName, (msg) => {
      if (msg.isGetData) {
        this.getTableData();
      }
    });
  },
  mounted() {
    bus.$on("ItemMonitorSqlServer", (msg) => {
      this.resetData();
      this.selectedMonID = msg;

      this.getTableData();
    });
  },
  computed: {
    
    tableDataFn() {
      let Statusfilter = {
        ['sleeping']:'睡眠',
        ['runnable']:'运行',
        ['']:''
      }
      this.tableData.forEach(item => {
        item.status = Statusfilter[item.status.trim()]
      });
      return this.tableData;
    },
  },
  methods: {
    getTableData() {
      this.loading = true;
      this.tableData = [];

      getJavaServerData("/Sp/sqlserversession", {
        ip: this.selectedMonID.IP,
        dbServername: this.selectedMonID.DB_SID,
        pageNum: this.postData.curPage,
        pageSize: this.postData.pageSize,
      }).then((res) => {
        let itemKey = Object.keys(res);
        let itemValue = Object.values(res);
        this.postData.dataTotal = Number(itemKey[0]);
        this.tableData = itemValue[0]?itemValue[0]:'';
      }).finally(()=>{
        this.loading = false
      });
    },
    getPaginationCurPage(val) {
      this.postData.curPage = val;
      this.getTableData();
    },
    getPaginationPageSize(val) {
      // 分页大小改变之后，重置当前页码为 1
      this.postData.curPage = 1;
      this.postData.pageSize = val;

      this.getTableData();
    },
    // getToolBarSearchIPArr(arr) {
    //   // 获取搜索IP关键字
    //   this.SearchIPArr = arr;

    //   this.postData.curPage = 1;
    //   if (this.SearchIPArr.length > 0) {
    //     this.postData.filters = {
    //       ip: this.SearchIPArr,
    //     };
    //   } else {
    //     this.postData.filters = {};
    //   }

    //   this.getTableData();
    // },
    // async getToolBarSearchIP(str) {
    //   // 获取搜索IP关键字
    //   this.SearchIP = str;
    //   this.postData.curPage = 1;

    //   await this.$store.commit("SqlServerTableSpace/SearchItem", this.SearchIP);
    //   // await this.$store.commit("SqlServerTableSpace/pagination",this.postData );
    //   this.getPagination(this.postData);
    //   this.tableData = await this.$store.getters[
    //     "SqlServerTableSpace/getSearchItem"
    //   ];
    //   this.postData.dataTotal = this.tableData.length;

    //   // this.postData.filters = {
    //   //     ip: this.SearchIP
    //   // };
    //   // this.getTableData();
    // },
    getChangeLie(val) {
      // 遍历表格列
      this.tableColumns.map((item, index) => {
        // 列的显示隐藏状态改变
        item.isShow = val.includes(item.label);
      });
    },
    resetData() {
      this.dataTotal = 0;
      this.tableData = [];
    },
  },
  watch: {},
  beforeDestroy() {
    // 页面关闭前，移除定时器任务
    bus.$emit("timerNewTodo", {
      name: this.timerName,
    });
  },
};
</script>

<style scoped lang="scss">
.el-table .warning-row {
  background: oldlace;
}
.el-table .success-row {
  background: #f0f9eb;
}
/deep/ .el-table thead {
  color: black;
}
</style>
